/**
 * Module:  module_spi_master
 * Version: 1v0
 * Build:   21a5617cdbae74496f778b2a7da0f0661babbd36
 * File:    spi_master.h
 *
 * The copyrights, all other intellectual and industrial 
 * property rights are retained by XMOS and/or its licensors. 
 * Terms and conditions covering the use of this code can
 * be found in the Xmos End User License Agreement.
 *
 * Copyright XMOS Ltd 2010
 *
 * In the case where this code is a modification of existing code
 * under a separate license, the separate license terms are shown
 * below. The modifications to the code are still covered by the 
 * copyright notice above.
 *
 **/                                   
///////////////////////////////////////////////////////////////////////////////
//
// SPI master (mode 3)
//
// Select lines are intentionally not part of API
// They are simple port outputs
// They depend on how many slaves there are and how they're connected
//

#ifndef _spi_master_h_
#define _spi_master_h_
#include <xs1.h>
#ifndef __XC__
#include <xccompat.h>
#endif

typedef struct spi_master_interface {
  clock blk1;
  clock blk2;
#ifdef __XC__
  out buffered port:8 mosi;
  out buffered port:8 sclk;
  in buffered port:8 miso;
#else
  port mosi;
  port sclk;
  port miso;
#endif
} spi_master_interface;


#define DEFAULT_SPI_CLOCK_DIV 8

// SPI clock frequency is fref/(2*spi_clock_div)
// where freq defaults to 100MHz
#ifdef __XC__
void spi_init(spi_master_interface &i, int spi_clock_div);
void spi_shutdown(spi_master_interface &i);
#else
void spi_init(spi_master_interface *i, int spi_clock_div);
void spi_shutdown(spi_master_interface *i);
#endif

// SPI master output
// big endian byte order
#ifdef __XC__
void spi_out_word(spi_master_interface &i, unsigned int data);
void spi_out_short(spi_master_interface &i, unsigned short data);
void spi_out_byte(spi_master_interface &i, unsigned char data);
void spi_out_buffer(spi_master_interface &i, 
                    const unsigned char buffer[], 
                    int num_bytes);
#else
void spi_out_word(spi_master_interface *i, unsigned int data);
void spi_out_short(spi_master_interface *i, unsigned short data);
void spi_out_byte(spi_master_interface *i, unsigned char data);
void spi_out_buffer(spi_master_interface *i,
                    const unsigned char buffer[],
                    int num_bytes);
#endif

// SPI master input
// big endian byte order
#ifdef __XC__
unsigned int spi_in_word(spi_master_interface &i);
unsigned short spi_in_short(spi_master_interface &i);
unsigned char spi_in_byte(spi_master_interface &i);
void spi_in_buffer(spi_master_interface &i,
                   unsigned char buffer[], 
                   int num_bytes);
#else
unsigned int spi_in_word(spi_master_interface *i);
unsigned short spi_in_short(spi_master_interface *i);
unsigned char spi_in_byte(spi_master_interface *i);
void spi_in_buffer(spi_master_interface *i,
                   unsigned char buffer[],
                   int num_bytes);
#endif



#endif
